import { nextTick, ref } from 'vue';
import { Input, message } from 'ant-design-vue';
import dayjs from 'dayjs';
import { useAuthStore } from '@/store/modules/auth';
import { isNonNegativeInteger } from '@/utils';
import { get, post } from '@/service/api/api';

let topFormModel;
let bottomFormModel;
const lotType = ref('input');
const lotids = ref([]);
const moduleNoSearchData = ref(null);
const authStore = useAuthStore();

const specificationList = ref([]);

function getSpecificationList() {
  get('/qms/agv/Grinder/findSize').then(({ data, error }) => {
    if (!error) {
      specificationList.value = data.map(item => {
        return {
          label: item.grinderSize,
          value: item.grinderSize
        };
      });
    }
  });
}

function getTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = now.getMonth() + 1;
  const day = now.getDate();
  const startTime = new Date(year, month - 1, day, 7, 30, 0);
  const endTime = new Date(year, month - 1, day, 19, 30, 0);
  if (topFormModel.value.date >= startTime && topFormModel.value.date <= endTime) {
    topFormModel.value.timePeriod1 = '07:30:00';
    topFormModel.value.valuetimePeriod2 = '19:30:00';
  } else {
    topFormModel.value.timePeriod1 = '19:30:00';
    topFormModel.value.timePeriod2 = '07:30:00';
  }
}

function addSpecification() {
  post('/qms/agv/Grinder/addSize', {
    grinderSize: topFormModel.value.specificationText
  }).then(({ data, error }) => {
    topFormModel.value.specificationText = 'SD';
    getSpecificationList();
  });
}

export default {
  url: '/qms/agv/Grinder/find',
  addUrl: '/qms/agv/Grinder/add',
  editUrl: '/qms/agv/Grinder/update',
  delUrl: '/qms/agv/Grinder/deleteBatch',
  editMethod: 'put',
  searchFormList: [
    {
      type: 'input',
      key: 'machineNumber',
      label: '机台号',
      placeholder: '请输入机台号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },
    {
      type: 'input',
      key: 'operator',
      label: '操作人',
      placeholder: '请输入操作人',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'dateRangePicker',
      key: 'time',
      label: '时间',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  searchParam: ref({
    machineNo: null,
    operator: null,
    state: null,
    time: null
  }),
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'machineNumber',
      dataIndex: 'machineNumber',
      title: '机台号',
      width: 100,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'date',
      dataIndex: 'date',
      title: '录入日期',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return dayjs(record.date).format('YYYY-MM-DD');
      }
    },
    {
      key: 'timePeriod1',
      dataIndex: 'timePeriod1',
      title: '开始时间',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'timePeriod2',
      dataIndex: 'timePeriod2',
      title: '结束时间',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'unit',
      dataIndex: 'unit',
      title: '单位',
      width: 80,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'specification',
      dataIndex: 'specification',
      title: '规格',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'grindNum',
      dataIndex: 'grindNum',
      title: '研磨数量',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'qualifiedNum',
      dataIndex: 'qualifiedNum',
      title: '合格数量',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'badDescriptions',
      dataIndex: 'badDescriptions',
      title: '不良描述',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'resolve',
      dataIndex: 'resolve',
      title: '解决情况',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'rate',
      dataIndex: 'rate',
      title: '合格率',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'grindTimes',
      dataIndex: 'grindTimes',
      title: '研磨次数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],

  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },

    {
      type: 'input',
      key: 'machineNumber',
      label: '机台号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'select',
      key: 'grindTimes',
      label: '研磨次数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '0',
          value: '0'
        },
        {
          label: '1',
          value: '1'
        },
        {
          label: '2',
          value: '2'
        },
        {
          label: '3',
          value: '3'
        },
        {
          label: '4',
          value: '4'
        },
        {
          label: '5',
          value: '5'
        },
        {
          label: '6',
          value: '6'
        }
      ]
    },
    {
      type: 'input',
      key: 'grindNum',
      label: '研磨数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'datePicker',
      key: 'date',
      label: '录入时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择录入时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      defaultValue: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
      showTime: true,
      change: () => {
        const now = new Date();
        const year = now.getFullYear();
        const month = now.getMonth() + 1;
        const day = now.getDate();
        const startTime = new Date(year, month - 1, day, 7, 30, 0);
        const endTime = new Date(year, month - 1, day, 19, 30, 0);
        if (topFormModel.value.date >= startTime && topFormModel.value.date <= endTime) {
          topFormModel.value.timePeriod1 = '07:30:00';
          topFormModel.value.valuetimePeriod2 = '19:30:00';
        } else {
          topFormModel.value.timePeriod1 = '19:30:00';
          topFormModel.value.timePeriod2 = '07:30:00';
        }
      }
    },
    {
      type: 'timePicker',
      key: 'timePeriod1',
      label: '开始时间',
      showSeconds: false,
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'timePicker',
      key: 'timePeriod2',
      label: '结束时间',
      showSeconds: false,
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'unit',
      label: '单位',
      showSeconds: false,
      span: 8,
      disabled: true,
      defaultValue: '支'
    },
    {
      type: 'select',
      key: 'specification',
      label: '规格',
      search: true,
      showSeconds: false,
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: specificationList
    },
    {
      type: 'searchInput',
      key: 'specificationText',
      label: '新增规格',
      defaultValue: 'SD',
      showSeconds: false,
      searchBtnText: '新增',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      handleSearch: () => {
        addSpecification();
      }
    },
    {
      type: 'input',
      key: 'qualifiedNum',
      label: '合格数量',
      showSeconds: false,
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'select',
      key: 'badDescriptions',
      label: '不良描述',
      search: true,
      showSeconds: false,
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '重分',
          value: '重分'
        },
        {
          label: '大小头',
          value: '大小头'
        },
        {
          label: '大小面',
          value: '大小面'
        },
        {
          label: '长短面',
          value: '长短面'
        },
        {
          label: '缺口',
          value: '缺口'
        },
        {
          label: '圆角',
          value: '圆角'
        }
      ]
    },
    {
      type: 'select',
      key: 'resolve',
      label: '解决情况',
      search: true,
      showSeconds: false,
      span: 8,
      defaultValue: '反磨',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '反磨',
          value: '反磨'
        },
        {
          label: '报废',
          value: '报废'
        }
      ]
    },
    {
      type: 'radio',
      key: 'dateClass',
      label: '班别',
      span: 6,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'A班',
          value: 'A'
        },
        {
          label: 'B班',
          value: 'B'
        }
      ]
    },
    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '作业员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '70vh',
  openModal(val) {
    getSpecificationList();
    nextTick(() => {
      if (val.operateType === 'add') {
        getTime();
      }
    });
  },
  modalClose() {
    lotType.value = 'input';
    moduleNoSearchData.value = null;
    lotids.value = [];
  }
};
